package com.qf;

import com.qf.mapper.EmpMapper;
import com.qf.mapper.PassengerMapper;
import com.qf.pojo.Emp;
import com.qf.pojo.Passenger;
import com.qf.utils.MyBatisUtils;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 动态SQL
 * @author lixu
 */
public class MybatisDynSql {

    /**
     *   <if 判断
     */
    @Test
    public void dynSQL(){
        EmpMapper mapper = MyBatisUtils.getMapper(EmpMapper.class);

        //String name = "     王      ";
        String name = null;
        //int age = 14;
        Integer age  = null;
        int salary = 30001;
/*        if(null != name){
            name=name.trim();
        }*/
        List<Emp> empList = mapper.findDynSql(name,age ,salary);

        empList.stream().forEach(System.out::println);
    }

    /**
     * 批量插入数据
     */
    @Test
    public void dynSQLBatchInsert(){
        EmpMapper mapper = MyBatisUtils.getMapper(EmpMapper.class);
        long startTime = System.currentTimeMillis();
        //耗时:972ms   3
/*        for (int i = 0; i < 2000; i++) {
            Emp emp = new Emp();
            emp.setName("name:" + i);
            emp.setAge(new Random().nextInt(100));
            emp.setSalary(1000 + i);
            mapper.insert(emp);
        }*/

        //耗时:341ms 1  耗时:331ms
        List<Emp> empList = new ArrayList<>();
        for (int i = 0; i < 2000; i++) {
            Emp emp = new Emp();
            emp.setName("name:" + i);
            emp.setAge(new Random().nextInt(100));
            emp.setSalary(1000 + i);
            empList.add(emp);
        }
        //一次
        mapper.insertList(empList); //1ms

        MyBatisUtils.commit();
        System.out.println("耗时:" + (System.currentTimeMillis() - startTime) + "ms");



    }
}
